<script lang="ts" setup>
import type { SystemOperateCategoriesApi } from '#/api/system/operateCategories';

import { ref } from 'vue';

import { IconifyIcon } from '@vben/icons';

import { Col, message, Row } from 'ant-design-vue';

import { router } from '#/router';

const props = defineProps<{
  color?: string; // 默认颜色
  icon?: string; // 默认图标
  items: SystemOperateCategoriesApi.OperateCategories[];
}>();

// 计算图标背景颜色（浅色）
const getItemBgColor = (itemColor?: string) => {
  // 如果有item特定颜色，使用它；否则使用组件颜色；最后默认使用主题色
  if (itemColor) {
    // 处理传入的颜色格式，确保正确应用
    // 如果是var()格式，直接添加透明度
    if (itemColor.includes('var(')) {
      return `${itemColor} / 0.15`;
    }
    // 如果是十六进制颜色格式，转换为rgba
    else if (itemColor.startsWith('#')) {
      let hex = itemColor.replace('#', '');
      // 处理3位或6位十六进制颜色
      if (hex.length === 3) {
        hex = hex
          .split('')
          .map((c) => c + c)
          .join('');
      }
      // 转换为RGB
      const r = Number.parseInt(hex.slice(0, 2), 16);
      const g = Number.parseInt(hex.slice(2, 4), 16);
      const b = Number.parseInt(hex.slice(4, 6), 16);
      // 返回rgba格式，透明度15%
      return `rgba(${r}, ${g}, ${b}, 0.15)`;
    }
    // 其他颜色格式（如hsl, rgb等）
    else {
      return `${itemColor} / 0.15`;
    }
  }
  // 使用默认主题色的浅色背景
  return `hsl(var(--primary) / 0.15)`;
};

// 获取图标颜色
const getIconColor = (itemColor?: string) => {
  if (itemColor) {
    return itemColor;
  }
  return props.color || 'hsl(var(--primary))';
};

// 响应式状态
const hoveredItem = ref('');

// 处理功能项点击事件
const handleItemClick = (
  item: SystemOperateCategoriesApi.OperateCategories,
) => {
  // 在实际应用中，这里应该导航到对应的页面
  if (item.componentName) {
    // 根据组件名称跳转路由
    router.push({ name: item.componentName });
  } else {
    message.info(`点击了${item.name}，功能未开放`);
  }
};
</script>
<template>
  <Row
    :gutter="{
      xs: 12,
      sm: 12,
      md: 14,
      lg: 14,
      xl: 16,
      xxl: 16,
    }"
  >
    <Col
      v-for="item in items"
      :key="item.id"
      :xs="12"
      :sm="6"
      :md="6"
      :lg="4"
      :xl="3"
      :xxl="3"
    >
      <div
        class="function-card"
        :class="{ 'function-card-hover': hoveredItem === item.id }"
        @click="handleItemClick(item)"
        @mouseenter="hoveredItem = item.id"
        @mouseleave="hoveredItem = 0"
      >
        <div
          class="icon-circle"
          :style="{ backgroundColor: getItemBgColor(item.color) }"
        >
          <IconifyIcon
            :icon="item.icon"
            class="item-icon"
            :color="getIconColor(item.color)"
          />
        </div>
        <span class="card-title">{{ item.name }}</span>
      </div>
    </Col>
  </Row>
</template>
<style lang="less" scoped>
.function-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 16px;
  background-color: hsl(var(--card));
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid hsl(var(--border));
  height: 150px;
  justify-content: center;
  position: relative;
  overflow: hidden;
  margin-bottom: 20px;

  &:hover {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px hsl(var(--shadow));
    border-color: transparent;
  }

  &:active {
    transform: translateY(0);
    box-shadow: 0 3px 8px hsl(var(--shadow));
  }
}

.icon-circle {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  transition: all 0.3s ease;
}

.function-card:hover .icon-circle {
  transform: scale(1.05);
}

.item-icon {
  font-size: 24px;
  transition: transform 0.3s ease;

  .function-card-hover & {
    transform: scale(1.1);
  }
}

.card-title {
  font-size: 14px;
  color: hsl(var(--foreground));
  font-weight: 500;
  text-align: center;
  transition: all 0.3s ease;
  line-height: 1.4;
  padding: 0 4px;
}

.function-card-hover {
  transform: translateY(-3px) !important;
  box-shadow: 0 8px 20px hsl(var(--shadow)) !important;
  border-color: hsl(var(--primary)) !important;
}

.function-card-hover .icon-circle {
  transform: scale(1.1);
}

.function-card-hover .card-title {
  transform: translateY(2px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .function-card {
    padding: 16px 12px;
    min-height: 100px;
  }

  .icon-circle {
    width: 48px;
    height: 48px;
    margin-bottom: 10px;
  }

  .item-icon {
    font-size: 20px;
  }

  .card-title {
    font-size: 13px;
  }
}

@media (max-width: 480px) {
  .function-card {
    padding: 14px 10px;
  }

  .icon-circle {
    width: 44px;
    height: 44px;
    margin-bottom: 8px;
  }

  .item-icon {
    font-size: 18px;
  }
}

/* 添加一些动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.function-card {
  animation: fadeIn 0.3s ease-out;
}

// 添加微妙的脉冲动画
@keyframes subtlePulse {
  0% {
    box-shadow: 0 0 0 0 transparent;
  }
  50% {
    box-shadow: 0 0 0 4px hsl(var(--primary) / 0.08);
  }
  100% {
    box-shadow: 0 0 0 0 transparent;
  }
}

.function-card:hover {
  animation: subtlePulse 2s infinite;
}

.function-card:nth-child(1) {
  animation-delay: 0.05s;
}
.function-card:nth-child(2) {
  animation-delay: 0.1s;
}
.function-card:nth-child(3) {
  animation-delay: 0.15s;
}
.function-card:nth-child(4) {
  animation-delay: 0.2s;
}
.function-card:nth-child(5) {
  animation-delay: 0.25s;
}
.function-card:nth-child(6) {
  animation-delay: 0.3s;
}
.function-card:nth-child(7) {
  animation-delay: 0.35s;
}
.function-card:nth-child(8) {
  animation-delay: 0.4s;
}
.function-card:nth-child(9) {
  animation-delay: 0.45s;
}
</style>
